package cn.iocoder.yudao.module.worker.controller.app.gift;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.worker.controller.app.gift.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.gift.WorkerGiftDO;
import cn.iocoder.yudao.module.worker.service.gift.WorkerGiftService;

@Tag(name = "用户 APP - 礼物")
@RestController
@RequestMapping("/worker/gift")
@Validated
public class AppWorkerGiftController {

    @Resource
    private WorkerGiftService giftService;

    @PostMapping("/create")
    @Operation(summary = "创建礼物")
    public CommonResult<Long> createGift(@Valid @RequestBody AppWorkerGiftSaveReqVO createReqVO) {
        return success(giftService.createGift(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新礼物")
    public CommonResult<Boolean> updateGift(@Valid @RequestBody AppWorkerGiftSaveReqVO updateReqVO) {
        giftService.updateGift(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除礼物")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteGift(@RequestParam("id") Long id) {
        giftService.deleteGift(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得礼物")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppWorkerGiftRespVO> getGift(@RequestParam("id") Long id) {
        WorkerGiftDO gift = giftService.getGift(id);
        return success(BeanUtils.toBean(gift, AppWorkerGiftRespVO.class));
    }

    @GetMapping("/getList")
    @Operation(summary = "获得礼物列表")
    public CommonResult<List<AppWorkerGiftRespVO>> getList() {
        List<WorkerGiftDO> giftList = giftService.getGiftList();
        return success(BeanUtils.toBean(giftList, AppWorkerGiftRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得礼物分页")
    public CommonResult<PageResult<AppWorkerGiftRespVO>> getGiftPage(@Valid AppWorkerGiftPageReqVO pageReqVO) {
        PageResult<WorkerGiftDO> pageResult = giftService.getGiftPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppWorkerGiftRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出礼物 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGiftExcel(@Valid AppWorkerGiftPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkerGiftDO> list = giftService.getGiftPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "礼物.xls", "数据", AppWorkerGiftRespVO.class,
                        BeanUtils.toBean(list, AppWorkerGiftRespVO.class));
    }

}